package org.fmblzf.library.volley;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.LruCache;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

/**
 * 2016/1/6.
 * zhaofeng
 * Volley封装类
 * 参考文章：http://blog.csdn.net/jdsjlzx/article/details/40738181
 */
public class VolleySingleton {

    private static final String TAG = "VolleySingleton";
    /**
     * 单例实体对象
     */
    private static VolleySingleton singleton = null;
    /**
     * 上下文对象
     */
    private Context mContext;
    /**
     * 请求队列
     */
    private RequestQueue mRequestQueue;

    /**
     * 私有构造器
     *
     * @param context
     */
    private VolleySingleton(Context context) {
        this.mContext = context;
        mRequestQueue = getRequestQueue();
    }

    /**
     * 外部接口方法，获取当前VolleySingleton的实例
     *
     * @param context
     * @return
     */
    public static synchronized VolleySingleton getSingleton(Context context) {
        if (singleton == null) {
            singleton = new VolleySingleton(context);
        }
        return singleton;
    }

    /**
     * 获取请求队列
     *
     * @return
     */
    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(mContext.getApplicationContext());
        }
        return mRequestQueue;
    }

    /**
     * 将请求添加到队列中，设置标签
     * Adds the specified request to the global queue, if tag is specified then
     * it is used else Default TAG is used.
     *
     * @param req
     * @param tag
     */
    public <T> void addToRequestQueue(Request<T> req, String tag) {
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        VolleyLog.d("Adding request to queue: %s", req.getUrl());
        getRequestQueue().add(req);
    }

    /**
     * 将请求添加到队列中,设置默认标签
     *
     * @param request
     * @param <T>
     */
    public <T> void addToRequestQueue(Request<T> request) {
        request.setTag(TAG);
//        request.setShouldCache(true);//设置是否缓存
        getRequestQueue().add(request);
    }

    /**
     * 取消队列中的请求
     * Cancels all pending requests by the specified TAG, it is important to
     * specify a TAG so that the pending/ongoing requests can be cancelled.
     *
     * @param tag
     */
    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }

    /**
     * 开始执行请求队列
     *
     * @param req
     */
    private void startVolley(Request req) {
        // 设置超时时间
        // req.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 1, 1.0f));

        // 将请求加入队列
        addToRequestQueue(req);
        // 开始发起请求
        mRequestQueue.start();
    }

}
